/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Media;
using System.Timers;
using System.Runtime.Serialization;

namespace SurveillanceDotNet.Model
{
    [Serializable]
    public class PlaySoundAlarmAction : AlarmAction
    {
        public string SoundFilePath { get; set; }
        private SoundPlayer _SoundPlayer;
        public bool LoopFile { get; set; }
        public double LoopFileTime { get; set; }
        private Timer _LoopFileTimer;

        public PlaySoundAlarmAction()
            : base()
        {
            _SoundPlayer = new SoundPlayer();
            this.LoopFile = false;
            _LoopFileTimer = new Timer();

            Status = "Not active";
        }

        public override void Action()
        {
            Status = "Active";
            _SoundPlayer.SoundLocation = SoundFilePath;

            if (!this.LoopFile)
            {
                _SoundPlayer.Play();
                Status = "Not active";
            }
            else
            {
                _LoopFileTimer.Interval = this.LoopFileTime;
                _LoopFileTimer.Elapsed += new ElapsedEventHandler(_LoopFileTimer_Elapsed);
                _SoundPlayer.PlayLooping();
                _LoopFileTimer.Start();
            }
        }

        void _LoopFileTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _LoopFileTimer.Stop();
            _LoopFileTimer.Elapsed -= _LoopFileTimer_Elapsed;
            _LoopFileTimer.Dispose();
            _SoundPlayer.Stop();
            Status = "Not Active";
        }

        #region ISerializable Implementation
        protected PlaySoundAlarmAction(SerializationInfo info, StreamingContext context) : base(info, context)
        {
            _SoundPlayer = new SoundPlayer();
            this.LoopFile = false;
            _LoopFileTimer = new Timer();

            SoundFilePath = info.GetString("SoundFilePath");
            LoopFile = info.GetBoolean("LoopFile");
            LoopFileTime = info.GetDouble("LoopFileTime");

            Status = "Not active";
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("SoundFilePath", SoundFilePath);
            info.AddValue("LoopFile", LoopFile);
            info.AddValue("LoopFileTime", LoopFileTime);
        } 
        #endregion
    }
}
